package com.joysuch.wwyt.mh.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.mh.bean.vo.MhMajorHazardPlanItemVO;
import com.joysuch.wwyt.mh.dto.MhMajorHazardTaskRecordItemDTO;
import com.joysuch.wwyt.mh.entity.MhMajorHazardPlanItem;
import com.joysuch.wwyt.mh.entity.MhMajorHazardTaskRecord;
import com.joysuch.wwyt.mh.entity.MhMajorHazardTaskRecordItem;
import com.joysuch.wwyt.mh.mapper.MhMajorHazardTaskRecordItemMapper;
import com.joysuch.wwyt.mh.service.MhMajorHazardTaskRecordItemService;
import com.joysuch.wwyt.mh.utils.DigitalConversionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zqw
 * @since 2022-05-24
 */
@Service
public class MhMajorHazardTaskRecordItemServiceImpl extends ServiceImpl<MhMajorHazardTaskRecordItemMapper, MhMajorHazardTaskRecordItem> implements MhMajorHazardTaskRecordItemService {

    @Autowired
    private MhMajorHazardTaskRecordItemMapper mhMajorHazardTaskRecordItemMapper;

    @Override
    public void batchSaveItem(Long id, List<MhMajorHazardTaskRecordItemDTO> itemDtoList) {
        List<MhMajorHazardTaskRecordItem> itemList = new ArrayList<>();
        itemDtoList.forEach(dto -> {
            MhMajorHazardTaskRecordItem item = new MhMajorHazardTaskRecordItem();
            BeanUtils.copyProperties(dto, item);
            item.setRecordId(id);
            //签字
            if (!CollectionUtils.isEmpty(dto.getPics())) {
                item.setPics(dto.getPics().stream().map(Object::toString).collect(Collectors.joining(Constant.SPLIT_COMMA)));
            }
            itemList.add(item);
        });
        saveOrUpdateBatch(itemList);
    }

    @Override
    public List<MhMajorHazardPlanItemVO> findHistoryTaskItemsByTaskId(Long taskId) {
        List<MhMajorHazardPlanItemVO> voList = new ArrayList<>();
        List<MhMajorHazardTaskRecordItem> itemList = mhMajorHazardTaskRecordItemMapper.findByTaskIdAndUserId(taskId, Context.getCurrentUserId());
        if (!cn.zlg.common.util.CollectionUtils.isEmpty(itemList)) {
            itemList.forEach(item -> {
                MhMajorHazardPlanItemVO itemVo = new MhMajorHazardPlanItemVO();
                BeanUtils.copyProperties(item, itemVo);
                voList.add(itemVo);
            });
        }
        return voList;
    }

    @Override
    public void batchSaveOverdueRecordItems(List<MhMajorHazardTaskRecord> records, Map<Long, Long> recordTaskIdMap, Map<Long, Long> taskPlanIdMap, Map<Long, List<MhMajorHazardPlanItem>> planItemMap, Date date) {
        List<MhMajorHazardTaskRecordItem> recordItems = new ArrayList<>();
        records.forEach(record -> {
            Long taskId = recordTaskIdMap.get(record.getId());
            Long planId = taskPlanIdMap.get(taskId);
            List<MhMajorHazardPlanItem> items = planItemMap.get(planId);
            for (int i = 0; i < items.size(); i++) {
                MhMajorHazardPlanItem item = items.get(i);
                MhMajorHazardTaskRecordItem recordItem = new MhMajorHazardTaskRecordItem();
                recordItem.setRecordId(record.getId());
                recordItem.setAppItemName("检查项" + DigitalConversionUtil.arabicNumToChineseNum(i + 1));
                recordItem.setCheckCriteria(item.getCheckCriteria());
                recordItem.setCheckFoundation(item.getCheckFoundation());
                recordItem.setCheckTerms(item.getCheckTerms());
                recordItem.setCheckWay(item.getCheckWay());
                recordItem.setOrgCode(item.getOrgCode());
                recordItem.setTenentId(item.getTenentId());
                recordItem.setCreateTime(date);
                recordItem.setUpdateTime(date);
                recordItem.setDeleteFlag(DeleteFlag.EFFECTIVE.getCode());
                recordItems.add(recordItem);
            }
        });
        saveOrUpdateBatch(recordItems);
    }

    @Override
    public List<MhMajorHazardTaskRecordItem> findByRecordIdAndResult(Long id, int result) {
        return mhMajorHazardTaskRecordItemMapper.findByRecordIdAndResult(id,result);
    }

    @Override
    public List<MhMajorHazardTaskRecordItem> getItemsByTaskId(Long taskId) {
        return mhMajorHazardTaskRecordItemMapper.getItemsByTaskId(taskId);
    }
}
